package com.ruoyi.recycleshop.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.recycle.domain.ShopManagement;
import com.ruoyi.recycle.dto.AttractDataQueryDTO;
import com.ruoyi.recycle.dto.ShopLabelsDTO;
import com.ruoyi.recycle.service.IShopManagementService;
import com.ruoyi.recycle.utils.OssUpload;
import com.ruoyi.recycle.vo.AttractDataVO;
import com.ruoyi.recycleshop.service.CashRegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/detail")
@Api(tags = "门店详情")
@Slf4j
public class ShopDetailController extends BaseController {
    @Autowired
    private CashRegisterService cashRegisterService;

    @Autowired
    private IShopManagementService shopManagementService;


    @GetMapping("/shopDataStatistics")
    @ApiOperation("门店数据统计")
    @ApiImplicitParam(name = "shopId", value = "门店ID")
    public R<AttractDataVO> shopDataStatistics(@RequestParam Long shopId) {
        return R.ok(cashRegisterService.shopDataStatistics(shopId));
    }

    @PostMapping("/shopDataStatisticsDetail")
    @ApiOperation("门店数据统计详情")
    public R<List<AttractDataVO>> shopDataStatisticsDetail(@RequestBody AttractDataQueryDTO queryDTO) {
        return R.ok(cashRegisterService.shopDataStatistics(queryDTO));
    }

    @GetMapping("/getShopDetail")
    @ApiOperation("获取门店详细信息")
    @ApiImplicitParam(name = "shopId", value = "门店ID")
    public R<ShopManagement> getShopDetail(@RequestParam Long shopId) {
        final ShopManagement shopManagement = shopManagementService.getById(shopId);
        if (shopManagement != null) {
            shopManagement.setShopPassword("");
        }
        return R.ok(shopManagement);

    }

    @PostMapping("/editShopInfo")
    @ApiOperation("修改门店信息")
    public R<Boolean> editShopInfo(@RequestBody ShopManagement shopManagement) {
        return R.ok(shopManagementService.saveOrUpdate(shopManagement));

    }

    @PostMapping("/uploadFile")
    @ApiOperation("上传文件")
    public R<String> uploadFile(@RequestParam("file") MultipartFile multipartFile) {
        try {
            final String fileUrl = OssUpload.uploadFile(multipartFile, ".jpg");
            return R.ok(fileUrl);
        } catch (Exception e) {
            log.error("上传资料失败,原因:{}", e.getMessage());
            return R.fail("上传失败");
        }

    }

    @PostMapping("/editLabels")
    @ApiOperation("编辑标签")
    public R addLabels(@RequestBody ShopLabelsDTO labels) {
        try {
            shopManagementService.addLabels(labels);
            return R.ok("更新标签成功");
        } catch (Exception e) {
            return R.fail("添加标签失败");
        }
    }


}
